package co.yixiang.modules.system.service.impl;

import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.modules.system.domain.UserRoll;
import co.yixiang.modules.system.dto.UserRollDto;
import co.yixiang.modules.system.dto.queryCriteria.UserRollQueryCriteria;
import co.yixiang.modules.system.mapper.UserRollMapper;
import co.yixiang.modules.system.service.UserRollService;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author zf
* @date 2021-09-24
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "userRoll")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserRollServiceImpl extends BaseServiceImpl<UserRollMapper, UserRoll> implements UserRollService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(UserRollQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<UserRoll> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(UserRollDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<UserRoll> queryAll(UserRollQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(UserRoll.class, criteria));
    }


    @Override
    public void download(List<UserRollDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserRollDto userRoll : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("业务员", userRoll.getOpenId());
            map.put("业务员名称", userRoll.getSalesman());
            map.put("用户名称", userRoll.getName());
            map.put("手机号", userRoll.getPhone());
            map.put("备注", userRoll.getRemark());
            map.put("贷款金额", userRoll.getLoan());
            map.put("卷状态", userRoll.getLoanStatus());
            map.put("消费卷状态", userRoll.getSpeedStatus());
            map.put("创建时间", userRoll.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
